<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('users');
        $this->load->helper('url');
        $this->load->helper('cookie');
    }
    
    public function logout(){
        $this->session->sess_destroy();
        redirect('main');
    }
    
    public function login(){
        $data = array();
        $data['title'] = 'Login | jajan.halodunia.com';
        $data['content'] = $this->load->view('user/login','',TRUE);
        $this->load->view('main',$data);
    }
    
    public function do_login(){
        $email = $this->input->post('email');
        $password = md5($this->input->post('password'));
        $user_id = $this->users->check_login($email,$password);
        if($user_id != 0){
            $this->session->set_userdata('logged', 'true');
            $this->session->set_userdata('user_id', $user_id);
            redirect('main');
        } else {
            $this->session->set_flashdata('login_status', TRUE);
            redirect('user/login');
        }
    }
    
    public function register(){
        $data = array();
        $data['title'] = 'Register | jajan.halodunia.com';
        $data['content'] = $this->load->view('user/register','',TRUE);
        $this->load->view('main',$data);
    }
    
    public function do_register(){
        $name = trim($this->input->post('name'));
        $email = trim($this->input->post('email'));
        $password = trim($this->input->post('password'));
        $conf_password = trim($this->input->post('cpassword'));
        $this->session->set_flashdata('reg_name',$name);
        $this->session->set_flashdata('reg_email',$email);
        if (strcmp($name,'') == 0){
            $this->session->set_flashdata('reg_status', 'Your name can\'t be blank');
            redirect('user/register');
        } else if (strcmp($password,'') == 0){
            $this->session->set_flashdata('reg_status', 'Your password can\'t be blank');
            redirect('user/register');
        } else if (strlen($password) < 6){
            $this->session->set_flashdata('reg_status', 'Your password can\'t be less than 6 character');
            redirect('user/register');
        } else if(strcmp($password, $conf_password) != 0){
            $this->session->set_flashdata('reg_status', 'Password doesn\'t match');
            redirect('user/register');
        } else {
            $data = array();
            $data['user_name'] = $name;
            $data['user_email'] = $email;
            $data['user_pwd'] = md5($password);
            //email check
            if($this->users->email_check($email)){
                $this->session->set_flashdata('reg_status', 'Your email has been registered');
                redirect('user/register');
            } else {
                $this->users->insert($data);
                $this->session->set_flashdata('reg_success', TRUE);
                redirect('user/login');
            }
        }
    }
}